package com.jlnk.androidfmc.fragment;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.WebViewActivity;
import com.jlnk.androidfmc.entity.ZNews;


import java.util.ArrayList;
import java.util.List;

public class ZnbNewFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public ZnbNewFragment() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_znbnew, container, false);

        // 假设你已经有一个方法来获取新闻数据
        final List<ZNews> newsList = getNewsData();

        // 为每个新闻项的图片设置点击事件监听器
        for (int i = 0; i < newsList.size(); i++) {
            final ZNews news = newsList.get(i); // 使用循环变量
            ImageView newsImageView = view.findViewWithTag("hotPhoto" + (i + 1));
            if (newsImageView != null) {
                // 为ImageView设置点击事件
                newsImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), WebViewActivity.class);
                        intent.putExtra("news", news); // 确保ZNews实现了Parcelable
                        startActivity(intent); // 使用startActivity而不是getContext().startActivity
                    }
                });
            }
        }

        return view;
    }


    private List<ZNews> getNewsData() {
        // 这里应该返回你的新闻数据列表
        // 示例数据，你需要替换为实际的数据获取逻辑
        List<ZNews> newsList = new ArrayList<>();
        newsList.add(new ZNews("农业发展", "file:///android_asset/images/n4.jpg", "农业发展大国"));
        newsList.add(new ZNews("新闻2标题", "file:///android_asset/images/news2.jpg", "新闻2内容"));
        newsList.add(new ZNews("新闻3标题", "新闻3图片URL", "新闻3内容"));
        newsList.add(new ZNews("新闻4标题", "新闻4图片URL", "新闻4内容"));
        newsList.add(new ZNews("新闻5标题", "新闻5图片URL", "新闻5内容"));
        return newsList;
    }


}